/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.securecreditlib.test;

import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.Test;

import com.ekeyman.securecreditlib.business.RegistryBO;
import com.ekeyman.securecreditlib.domain.Registry;
import com.ekeyman.securecreditlib.service.EkeymanService;
import com.ekeyman.securecreditlib.util.SecureCreditUtils;
import com.ekeymanlib.dto.EncryptionKeys;

public class AddSafetywordUT {
	
	private static final String[] SPRING_CONFIG_FILES = new String[]{"applicationContext-lib.xml"};
	ClassPathXmlApplicationContext ctx;
	
	private EkeymanService ekeymanService;
	private RegistryBO registryBO;
	
	/*
	 * A safetyword protects an application from unauthorized execution. For example, when
	 * the ekeyman administrator generates a new private key all subsequent encryption/decryption
	 * will fail. This could prove disastrous as new database entities are saved with bogus
	 * encryption keys. 
	 * 
	 * A clear text safetyword is stored in the registry along with it's encrypted counterpart known as
	 * the cipher text safetyword. When an application starts, it retrieves the safetywords and decrypts
	 * the cipher safety word. At this point, the clear text safetyword should equal the decrypted 
	 * cipher safety word. If it does, then the application continues as normal. If it doesn't, then
	 * the application should prevent further use. 
	 * 
	 * So, if the ekeyman administrator generates a new private key the two values will not match. Once the
	 * new private key is entered into the registry the values should match.
	 * 
	 */
	
	@Test
	public void testAddSafetyword() throws Exception{
		
		System.out.println("testAddSafetyword entered...");
		
		Registry registry = getRegistryBO().getRegistry();
		
		if(registry != null && registry.getPublicKey() != null  && !registry.getPublicKey().equalsIgnoreCase("")){
				
			EncryptionKeys encryptionKeys = getEkeymanService().createEncryptionKeys(
					"safetyword", 
					registry.getPublicKey(), 
					registry.getPrivateKey(),
					16,16,16,1024,
					"App Server");
			
			String clearSafetyword = "Safetyword prevents unauthorized program execution";
			
			String cipherSafetyword = SecureCreditUtils.encrypt(encryptionKeys, clearSafetyword);
			
			registry.setClearSafetyword(clearSafetyword);
			registry.setCipherSafetyword(cipherSafetyword);
			getRegistryBO().saveRegistry(registry);
		}
		
		
		System.out.println("testAddSafetyword...clearSafetyword: " + registry.getClearSafetyword());
		System.out.println("testAddSafetyword...cipherSafetyword: " + registry.getCipherSafetyword());		
		
		System.out.println("testAddSafetyword exited...");
	}
	
	
	@BeforeSuite
	public void beforeSuite() {
		System.out.println("Before suite...");
		
		ctx = new ClassPathXmlApplicationContext(SPRING_CONFIG_FILES);
		setEkeymanService((EkeymanService)ctx.getBeanFactory().getBean("ekeymanService"));
		setRegistryBO((RegistryBO)ctx.getBeanFactory().getBean("registryBO"));
	}
	
	@AfterSuite
	public void afterSuite() {
		System.out.println("After suite...");
	}


	public EkeymanService getEkeymanService() {
		return ekeymanService;
	}


	public void setEkeymanService(EkeymanService ekeymanService) {
		this.ekeymanService = ekeymanService;
	}


	public void setRegistryBO(RegistryBO registryBO) {
		this.registryBO = registryBO;
	}


	public RegistryBO getRegistryBO() {
		return registryBO;
	}
	


}
